//
//  CartoonMyDownLoadTableViewCell.m
//  Cartoon
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "CartoonMyDownLoadTableViewCell.h"

@interface CartoonMyDownLoadTableViewCell ()



@end

@implementation CartoonMyDownLoadTableViewCell
- (void)dealloc
{
    [self.myProgressView removeObserver:self forKeyPath:@"progress"];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _page = 0;
        self.myDownLabel = [[UILabel alloc] init];
        self.myImageView = [[UIImageView alloc] init];
        self.myNameLabel = [[UILabel alloc] init];
        self.myProgressView = [[UIProgressView alloc] init];
        self.myPageLabel = [[UILabel alloc] init];
        self.myNumberLabel = [[UILabel alloc] init];
//        self.myBeginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
//        [self.myBeginButton addTarget:self action:@selector(beginButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.myBeginButton setTitle:@"开始" forState:UIControlStateNormal];
//        self.myBeginButton.titleLabel.text = @"开始";
//        self.myBeginButton.frame = CGRectMake(self.bounds.size.width - 100, 20, 60, 20);
//        self.myBeginButton.layer.borderWidth = 1;
//        self.myBeginButton.layer.cornerRadius = 5;
        
        [self.myProgressView addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];
        
//        [self.contentView addSubview:_myBeginButton];
        
        [self.contentView addSubview:_myPageLabel];
        [self.contentView addSubview:_myNumberLabel];
        [self.contentView addSubview:_myDownLabel];
        [self.contentView addSubview:_myImageView];
        [self.contentView addSubview:_myNameLabel];
        [self.contentView addSubview:_myProgressView];
    }
    return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
//    NSLog(@"%@",[change objectForKey:@"new"]);
    NSInteger down = [[change objectForKey:@"new"] floatValue] * 100 / 1;
    self.myDownLabel.text = [NSString stringWithFormat:@"%ld %@",down,@"%"];
    
//    NSLog(@"%@",self.myDownLabel.text);
//    NSLog(@"%ld",self.page);
    if ([[[self.myDownLabel.text componentsSeparatedByString:@"."] firstObject] isEqualToString:@"100 %"] && !_over) {
        _page ++;
    }
    self.myNumberLabel.text = [NSString stringWithFormat:@"%ld/%@",_page,[[self.myNumberLabel.text componentsSeparatedByString:@"/"] lastObject]];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.myImageView.frame = CGRectMake(10, 10, self.bounds.size.height - 20, self.bounds.size.width / 5);
    self.myNameLabel.frame = CGRectMake(self.myImageView.frame.size.width + 20, self.bounds.size.height / 4, self.bounds.size.width - self.myImageView.frame.size.width - 20, self.bounds.size.height / 4);
    self.myProgressView.frame = CGRectMake(self.myImageView.frame.size.width + 20, self.bounds.size.height / 2, self.bounds.size.width - self.myImageView.frame.size.width - 20 - 80, 2);
    self.myDownLabel.frame = CGRectMake(self.myProgressView.frame.origin.x + self.myProgressView.frame.size.width + 20, self.bounds.size.height / 2 - 10,60, 20);
    self.myPageLabel.frame = CGRectMake(self.myProgressView.frame.origin.x, self.myProgressView.frame.origin.y + 2, 200, 20);
    self.myNumberLabel.frame = CGRectMake(self.myProgressView.frame.origin.x + self.myProgressView.frame.size.width - 60, self.myProgressView.frame.origin.y - 20, 60, 20);
    self.myNumberLabel.textAlignment = NSTextAlignmentCenter;
    
//    self.myBeginButton.frame = CGRectMake(self.bounds.size.width - 100, self.myProgressView.frame.origin.y + 20, 60, 20);
    
    
}



//- (void)beginButtonAction:(UIButton *)btn
//{
//    [self.delegate beginButtonActionWithCell:self];
//}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
